import 'package:flutter/material.dart';
import 'package:ileddemo/code/ui/base/BaseWidget.dart';
import 'package:ileddemo/code/ui/base/TitleBarWidget.dart';
import 'package:ileddemo/code/ui/source/ShowSourceWidget.dart';
import 'package:bot_toast/bot_toast.dart';
import '../../../generated/l10n.dart';
import '../../ble/protocol/AutoConnectUtil.dart';
import '../../ble/protocol/base/SourceInterface.dart';
import '../../ble/protocol/iledcolor/BleBean.dart';
import '../../ble/protocol/iledcolor/source/ColorSourceInterface.dart';
import '../../ble/protocol/iledcolor/source/SourceManager.dart';
import 'EditWidget.dart';

class SourcePage extends StatefulWidget {
  const SourcePage({super.key});

  @override
  State<SourcePage> createState() => _SourcePageState();
}

class _SourcePageState extends State<SourcePage> {
  Size getScreenSize() {
    var lastConnectBleBean = AutoConnectUtil.instance.getLastConnectBleBean();
    BleBean bleBean = BleBean(bean: lastConnectBleBean);
    return bleBean.getSize();
  }

  List<ColorSourceInterface> getSourceList() {
    Size screenSize = getScreenSize();
    List<ColorSourceInterface> list = [];
    var map = SourceManager.instance.getSource();
    for (var element in map.values) {
      if (element.screenSize == screenSize) {
        list.add(element);
      }
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    var sourceList = getSourceList();
    return BaseWidget(
      child: Column(
        children: [
          TitleBarWidget(title: S.of(context).str_id_33),

          Expanded(
            child: Container(
              margin: EdgeInsets.all(10),
              child: GridView.count(
                crossAxisCount: 2,
                childAspectRatio: 1,
                mainAxisSpacing: 10,
                crossAxisSpacing: 10,
                children: List.generate(sourceList.length, (index) {
                  return GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () async {
                      var a = await showDialog(
                        context: context,
                        builder: (context) {
                          return EditWidget(source: sourceList[index]);
                        },
                      );
                      setState(() {});
                    },
                    child: Container(
                      decoration: BoxDecoration(color: Colors.blueGrey, borderRadius: BorderRadius.circular(10)),
                      child: ShowSourceWidget(source: sourceList[index]),
                    ),
                  );
                }),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
